#!/bin/bash



echo "Executing script by: chenguiming 24055060324"

SOURCE_FILE="scores.txt"

OUTPUT_DIR="students"

if [ ! -f "$SOURCE_FILE" ]; then

    echo "Error: Source file '$SOURCE_FILE' not found!"

    exit 1

fi

mkdir -p "$OUTPUT_DIR"

while read name gender id java_subj java_score python_subj python_score cpp_subj cpp_score linux_subj linux_score; do

    total=$(( java_score + python_score + cpp_score + linux_score ))

    average=$(echo "scale=1; $total / 4" | bc)

    output_file="$OUTPUT_DIR/$name.txt"

    echo "name: $name" > "$output_file"

    echo "id: $id" >> "$output_file"

    echo "gender: $gender" >> "$output_file"

    echo "scores: $java_subj $java_score $python_subj $python_score $cpp_subj $cpp_score $linux_subj $linux_score" >> "$output_file"

    echo "total: $total" >> "$output_file"

    echo "average: $average" >> "$output_file"

done < "$SOURCE_FILE"

echo "All student files have been created in the '$OUTPUT_DIR' directory."

exit 0


